<template>
    <div class="wrap">
        <el-form ref="loginRef" :rules="rules" :model="userInfo" class="login" label-width="80px">
            <el-form-item label="用户名" prop="username">
                <el-input v-model="userInfo.username" clearable></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input v-model="userInfo.password" clearable show-password></el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="login(loginRef)" class="btn">登陆</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script setup lang="ts">
import { reactive, ref } from 'vue'
//引入路由控制器的方法
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'
import { userlogin } from '@/request/api'
import { onMounted } from 'vue'

//设定路由控制器的方法
let router = useRouter()

let store = useStore()

let userInfo = reactive({
    username: "",
    password: ""
})
let rules = reactive<FormRules>({
    username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur' }
    ]

})

let loginRef = ref<FormInstance>();
const login = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((val: boolean) => {
        if (val) {
            console.log(userInfo);
            userlogin(userInfo).then(res => {
                if (res.code == 200) {
                    // 存储到本地
                    store.dispatch("changeUser",res.list)
                    router.push("/home")
                    ElMessage({
                        type: 'success',
                        message: res.msg,
                    })
                } else {
                    ElMessage({
                        type: 'error',
                        message: res.msg,
                    })
                }
            })

        }

    })
    
}

let login1 = (e:any) => {
            if (e.keyCode === 13) {
                login(loginRef.value)
            }
        }
         //禁用浏览器返回按钮
let banBack = () => {
            history.pushState(null, "null", document.URL);
            window.addEventListener("popstate", function () {
                history.pushState(null, "null", document.URL);
            });
        }
onMounted(()=>{
    window.addEventListener("keydown", login1, true)
    banBack()
})
</script>
<style lang="less">
.wrap {
    width: 100vw;
    height: 100vh;
    position: relative;
    background-image: url(../assets/img/1.jpg);
    background-size: 100% 100%;


    .login {
        position: absolute;
        width: 450px;
        height: 150px;
        left: 77%;
        top: 55%;
        margin: -175px 0 0 -225px;
         background-color: rgba(255,255,255,0.4);
        padding: 20px;
    }
    .btn{
        background-color: #e09f6a;
        color: #fff;
        border: 0;
        position: absolute;
        left: 50%;
        width: 150px;
        margin-left: -50%;
        margin-top: 40px;
        
    }
}
</style>